<?php
class Relation
{
	/**
	* = operador
	*/
	function Equal(){return 0;}
	
	/**
	* < operador
	*/
	function  LessThan (){return  1;}
	
	/**
	* > operador
	*/
	function GreaterThan (){return 2;}
	
	/**
	* <= operador
	*/
	function LessOrEqualThan(){return   3;}
	/**
	* >= operador
	*/
	function GreaterOrEqualThan (){return  4;}
	/**
	* != operador
	*/
	function NotEqual (){return  5;}
}

class FilterOperator
{
	function AND_operator() {return 'AND';}
	function OR_operator() {return 'OR';}
}

class QueryFilter
{
	var $arrayFields;
	
	var $command;
	
	var $field;
	
	/**
	 * Adicionar Filtro
	 *
	 * @param FieldSchema $field
	 * @param mixed $value
	 * @param FilterOperator $operator
	 * @param Relation $relation	
	 */
	function addFilter($field, $value, $operator = 'AND', $relation = 0)
	{
		switch ($relation) 
		{
			case 1: $relation = '<'; break;
			case 2: $relation = '>'; break;
			case 3: $relation = '>='; break;
			case 4: $relation = '<='; break;
			case 5: $relation = '!='; break;
			default: $relation = '='; break;
		}
		$this->arrayFields[$field] = array($operator, $relation, $value);
	}
	
	function addCommand($command)
	{
		$this->command = $command; 
	}	
	
	function addField($command)
	{
		$this->field = $command; 
	}
}

class Query
{
	/**
	 * Consulta SQL
	 *
	 * @var string
	 */
	var $SQL;
	/**
	 * Array com valores da consulta
	 *
	 * @var array
	 */
	var $prepare;
	
	/**
	 * Construtor
	 *
	 * @param string $sql
	 * @param array $prepare
	 * @return Query
	 */
	function Query($sql = '', $prepare = null)
	{
		$this->SQL = $sql;
		$this->prepare = $prepare;
	}
}

class QueryHelp
{
	/**
	 * tabela
	 *
	 * @var Table
	 */
	var $_tableSchema;
	
	/**
	 * Construtor
	 *
	 * @param TableSchema $table
	 * @return QueryHelper
	 */
	function QueryHelp($tableSchema)
	{
		$this->_tableSchema = $tableSchema;
	}
	
	/**
	 * INSERT
	 *
	 * @param array $arrayValues
	 * @return Query
	 */
	function Insert($arrayValues)
	{
		if (!$this->_tableSchema)
		{
			new CMSException("1054","Informar nome tabela", "ERRO - QueryHelp");
		}
		$arrayPrepare = array();
		$prepareOperator = '?';
		$sql = 'INSERT INTO ' . $this->_tableSchema . ' (' ;
		$sqlValues = ' VALUES (';
		foreach ($arrayValues as $index => $field) 
		{
				$arrayPrepare[] = $field;
				$sql .= ' ' . $index . ', ';
				$sqlValues .= ' ' . $prepareOperator . ', ';
			
		}
		$sql = substr($sql, 0, -2);
		$sqlValues = substr($sqlValues, 0, -2);
		$sql .= ' )' . $sqlValues . ' )';
		
		return new Query($sql, $arrayPrepare);
	}
	
	/**
	 * UPDATE
	 *
	 * @param array $arrayValues
	 * @param QueryFilter $queryFilter
	 * @return Query
	 */
	function Update($arrayValues, $queryFilter = null)
	{
		if (!$this->_tableSchema)
		{
			new CMSException("1054","Informar nome tabela", "ERRO - QueryHelp");
		}
		$prepareOperator = '?';
		$sql = 'UPDATE ' . $this->_tableSchema . ' SET ' ;
		foreach ($arrayValues as $index => $field) 
		{
				$arrayPrepare[] = $field;
				$sql .= ' ' . $index . ' = ' . $prepareOperator . ', ';
			
		}
		$sql = substr($sql, 0, -2);
		if ($queryFilter) 
		{
			$sql .= ' WHERE 1 = 1 ';
			foreach ($queryFilter->arrayFields as $fieldName => $fields) 
			{
				$sql .= $fields[0] . ' ' . $fieldName . ' ' . $fields[1] . ' ' . $prepareOperator . ' ';
				$arrayPrepare[] = $fields[2];
			}
			
			if($queryFilter->command)
			{
				$sql .= " " . $queryFilter->command;
			}
		}
		return new Query($sql, $arrayPrepare);
	}
	
	/**
	 * DELETE
	 *
	 * @param QueryFilter $queryFilter
	 * @return Query
	 */
	function Delete($queryFilter = null)
	{
		$prepareOperator = '?';
		$sql = 'DELETE FROM ' . $this->_tableSchema;
		if ($queryFilter) 
		{
			$sql .= ' WHERE 1 = 1 ';
			foreach ($queryFilter->arrayFields as $fieldName => $fields)
			{
				$sql .= $fields[0] . ' ' . $fieldName . ' ' . $fields[1] . ' ' . $prepareOperator . ' ';
				$arrayPrepare[] = $fields[2];
			}
			if($queryFilter->command)
			{
				$sql .= " " . $queryFilter->command;
			}
		}
		return new Query($sql, $arrayPrepare);
	}
	
}


?>